package com.qian.ddj.web.controller;

import com.qian.api.ddj.model.Business;
import com.qian.api.ddj.model.BusinessGroup;
import com.qian.api.ddj.service.IBusinessGroupService;
import com.qian.api.ddj.service.IBusinessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/ddj/businessGroup")
@Api(tags = "商户组管理")
public class BusinessGroupController {

    @Autowired
    private IBusinessGroupService businessGroupService;
    @Autowired
    private IBusinessService businessService;

    @GetMapping("/list/{id}")
    @ApiOperation("商户组列表")
    public List<BusinessGroup> list(@PathVariable Integer id) {
        BusinessGroup businessGroup = new BusinessGroup();
        businessGroup.setChannelId(id);
        List<BusinessGroup> businessGroups = businessGroupService.findAll(businessGroup);
        return businessGroups;
    }

    @PostMapping()
    @ApiOperation("保存商户组")
    public BusinessGroup save(BusinessGroup businessGroup) {
        businessGroup = businessGroupService.save(businessGroup);
        return businessGroup;
    }

    @GetMapping("/stop/{id}")
    public void stop(@PathVariable Integer id) {
        List<Business> businesses = businessService.findAllBusinessByGroupId(id);
        Set<Integer> businessIds = new HashSet<>();
        for (Business business : businesses) {
            businessIds.add(business.getId());
        }
        businessService.stop(businessIds);
    }

    @GetMapping("/start/{id}")
    public void start(@PathVariable Integer id) {
        List<Business> businesses = businessService.findAllBusinessByGroupId(id);
        Set<Integer> businessIds = new HashSet<>();
        for (Business business : businesses) {
            businessIds.add(business.getId());
        }
        businessService.start(businessIds);
    }
}
